/*
 * 创建时间：17-9-23
 * 作者：sce
 * 功能描述：提供post请求服务
 * 修改时间：
 * 测试用例：@/components/Test.vue
 */

import PromisePost from './post.promise';
import {
	getRemoteAddress
} from './ip.js'

function searchMap(key) {
	let map = new Map();
	map.set("TEAM_LISTALL", "team.listall");
	map.set("MENU_ADD", "menu.add");
	map.set("USER_LOGIN", "user.login");
	map.set("USER_REGISTER", "user.register");

	map.set('USER_GET', 'user.get')

	map.set("DOWNLOAD_ADD", "download.add");
	map.set("DOWNLOAD_LISTPAGE", "download.listpage");
	map.set("DOWNLOAD_DEL", "download.del");
	map.set("DOWNLOAD_EDIT", "download.edit");
	//获取发布的新版本（安卓/iOS）
	map.set('DOWNLOAD_FINDURL', 'download.findurl');

	map.set("FEEDBACK_ADD", "feedback.add");
	map.set("FEEDBACK_LISTPAGE", "feedback.listpage");
	map.set("FEEDBACK_DEL", "feedback.del");
	map.set("FEEDBACK_EDIT", "feedback.edit");

	map.set("TIMELINE_ADD", "timeline.add");
	map.set("TIMELINE_LISTPAGE", "timeline.listpage");
	map.set("TIMELINE_DEL", "timeline.del");
	map.set("TIMELINE_EDIT", "timeline.edit");

	//查询发布的功能
	map.set("ABILITY_ADD","ability.add");
	map.set("ABILITY_EDIT","ability.edit");
	map.set("ABILITY_LISTPAGE","ability.listpage");
	map.set("ABILITY_DEL","ability.del");
	map.set("ABILITY_ISPUBLISH","ability.ispublish");
	map.set('ABILITY_LIST', 'ability.list');
	//毕业去向管理
	map.set("EXCELLENT_ADD", "excellent.add");
	map.set("EXCELLENT_EDIT", "excellent.edit");
	map.set("EXCELLENT_LISTPAGE", "excellent.listpage");
	map.set("EXCELLENT_DEL", "excellent.del");
	map.set("EXCELLENT_ISPUBLISH", "excellent.ispublish");

	map.set("ARTICLETYPE_ADD", "articletype.add");
	map.set("ARTICLETYPE_EDIT", "articletype.edit");
	map.set("ARTICLETYPE_LISTPAGE", "articletype.listpage");
	map.set("ARTICLETYPE_DEL", "articletype.del");
	map.set("ARTICLETYPE_LISTALL", "articletype.listall");
	map.set("ARTICLETYPE_ISPUBLISH", "articletype.ispublish");

	map.set("DIARY_ADD", "diary.add");
	map.set("DIARY_EDIT", "diary.edit");
	map.set("DIARY_LISTPAGE", "diary.listpage");
	map.set("DIARY_DEL", "diary.del");
	map.set("DIARY_ISPUBLISH", "diary.ispublish");
	map.set("DIARY_GET", "diary.get");

	//团队介绍管理
	map.set("TEAM_ADD","team.add");
	map.set("TEAM_EDIT","team.edit");
	map.set("TEAM_LISTPAGE","team.listpage");
	map.set("TEAM_DEL","team.del");
	map.set("TEAM_GET","team.get");
	map.set("TEAM_ISPUBLISH","team.ispublish");

	//用户管理
	map.set("USER_LOGIN","user.login");
	map.set("USER_REGISTER","user.register");
	map.set("USER_GET","user.get");
	map.set("USER_DEL","user.del");
	map.set("USER_UPDATEINFO","user.updateinfo");
	map.set("USER_UPDATEPSW","user.updatepsw");
	map.set("USER_LISTPAGE","user.listpage");
	map.set("MEMBER_EDIT","member.edit");
	map.set("MEMBER_ADD","member.add");
	map.set("MEMBER_LISTPAGE","member.listpage");
	map.set("MEMBER_FIND","member.get");
	map.set("MEMBER_LISTALL","member.listall");
	//申请加入实验室
	map.set("APPLY_ADD","apply.add");
	//项目展示
	map.set("PROJECT_ADD","project.add"); //新增
	map.set("PROJECT_LISTALL","project.listall");//list
	map.set("PROJECT_DEL","project.del");//删除
	map.set("PROJECT_UPDATE","project.update");//编辑
	map.set("PROJECT_LISTPAGE","project.listpage") //
	//周志
	map.set("WEEKLY_EDIT","weekly.edit");//
	map.set("WEEKLY_ADD","weekly.add");//
	map.set("WEEKLY_GET","weekly.get");//
	map.set("WEEKLY_DEL","weekly.del");
	map.set("WEEKLY_LISTPAGE","weekly.listpage");//
	map.set("WEEKLY_EDIT","weekly.edit");
	map.set("WEEKLY_VERIFY","weekly.verify");
	//技术日志
	map.set("LOG_ADD","log.add");
	map.set("LOG_LISTALL","log.listall");
	map.set("LOG_GET","log.get");
	map.set("LOG_DEL","log.del");
	map.set("LOG_LISTPAGE","log.listpage");
	map.set("LOG_EDIT","log.edit");

	map.set('MENU_ADD','menu.add');
    map.set('MENU_LISTPAGE','menu.listpage');
    map.set('MENU_DELATE','menu.del');
   
	map.set("APPLY_DEL","apply.del");
	map.set("APPLY_LISTALL","apply.listall");
	map.set("APPLY_LISTPAGE","apply.listpage");
	map.set("APPLY_VERIFY","apply.verify");
	map.set("APPLY_GET","apply.get");
	return map.get(key);
}


function urlConstruct(key) {
	let url = searchMap(key);
	while (url.indexOf(".") >= 0) {
		url = url.replace(".", "/");
	}
	return url;
}

export function postDataToServer({
	url,
	parameter = {},
	header = {}
}) {
	let remoteAdress = getRemoteAddress();
	let promisePost = new PromisePost();
	promisePost.uri = remoteAdress + urlConstruct(url);
	promisePost.method =  urlConstruct(url);
	promisePost.options = parameter;
	promisePost.header = header;
	promisePost.createDesign();
	return promisePost.ajaxPromise();
}